﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace csharpcore7_ch14_ConcurrencyAndAsynchronyWPF
{
    /// <summary>
    /// chapt14_2_12SynchronizationContext.xaml 的交互逻辑
    /// </summary>
    public partial class chapt14_2_12SynchronizationContext : Window
    {
        SynchronizationContext _uiSyncContext;
        public chapt14_2_12SynchronizationContext()
        {
            InitializeComponent();
            //Capture the synchronizaton context for the current UI thread:
            _uiSyncContext = SynchronizationContext.Current;
            new Thread(Work).Start();
        }

        void Work()
        {
            Thread.Sleep(5000); //模拟长时间任务
            UpdateMessage("The answer");
            void UpdateMessage(string message)
            {
                Action action = () => textMessage.Text = message;
                _uiSyncContext.Post(_ => textMessage.Text=message,null);
            }
        }
    }
}
